package com.gxa.zhonglong.service.impl;

import com.gxa.zhonglong.common.ExceptionCode;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.mapper.GoodsMapper;
import com.gxa.zhonglong.mapper.GoodsMaterialMapper;
import com.gxa.zhonglong.pojo.Goods;
import com.gxa.zhonglong.pojo.GoodsMaterial;
import com.gxa.zhonglong.pojo.GoodsMaterialDetail;
import com.gxa.zhonglong.pojo.Where;
import com.gxa.zhonglong.service.GoodsMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class GoodsMaterialServiceImp implements GoodsMaterialService {

    @Autowired
    GoodsMaterialMapper goodsMaterialMapper = null;

    @Autowired
    GoodsMapper goodsMapper = null;

    @Override
    public void insertGoodsMaterial(GoodsMaterial goodsMaterial) {
        goodsMaterialMapper.insertGoodsMaterial(goodsMaterial);
    }

    @Override
    public void insertGoodsMaterialD(Goods goods) {
        goodsMaterialMapper.insertGoodsMaterialD(goods);
    }


    @Override
    public List<Map<String, Object>> selectByReceiptNumberGoodsMaterial(String receiptNumber) {
        List<Map<String, Object>> maps = goodsMaterialMapper.selectByReceiptNumberGoods(receiptNumber);
        return maps;
    }

    @Override
    public List<Map<String, Object>> selectByReceiptNumberGoods(String receiptNumber) {
        List<Map<String, Object>> maps = goodsMaterialMapper.selectByReceiptNumberGoods(receiptNumber);
        return maps;
    }

    @Override
    public List<Map<String, Object>> selectByWhere(Where where) {

        Integer page = where.getPage();
        Integer size = where.getSize();
        where.setPage((page-1)*size);
        List<Map<String, Object>> maps = goodsMaterialMapper.selectByWhere(where);
        return maps;
    }

    @Override
    public List<Map<String, Object>> selectByWhereNoPage(Where where) {
        where.setPage(null);
        List<Map<String, Object>> maps = goodsMaterialMapper.selectByWhere(where);
        return maps;
    }

    @Override
    public void updateByReceiptNumber(GoodsMaterial goodsMaterial) {
        goodsMaterialMapper.updateByReceiptNumber(goodsMaterial);
    }

    @Override
    public void deleteByReceiptNumberGoodsMaterial(String receiptNumber) {
        goodsMaterialMapper.deleteByReceiptNumberGoodsMaterial(receiptNumber);
    }


    @Override
    public void deleteByNumber(String number) throws BusinessException {
        List<Map<String, Goods>> maps = goodsMapper.selectByNumber(number);
        if (maps.size()>0){
            goodsMaterialMapper.deleteByNumber(number);
        }else {
            throw new BusinessException(ExceptionCode.DELETE_FALSE);
        }

    }
}
